package com.knoya.chejian.pda;


        import android.os.Bundle;
        import java.util.Map;
        import android.app.Activity;
        import android.content.BroadcastReceiver;
        import android.content.Context;
        import android.content.Intent;
        import android.content.IntentFilter;
        import android.view.Menu;
        import android.view.View;
        import android.widget.Button;
        import android.widget.EditText;
        import android.widget.RadioGroup;
        import android.widget.TextView;
        import android.widget.Toast;

public class Main2Activity extends Activity {


    private TextView tv_broadcast_result;
    private EditText et_emulator_result;
    private RadioGroup radio_group;
    private Button btn_reset;
    Context mContext;
    private BroadcastReceiver  mReceiver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        mContext = getApplicationContext();
        initView();
        mReceiver= new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                final String scanResult_1=intent.getStringExtra("SCAN_BARCODE1");
                final String scanResult_2=intent.getStringExtra("SCAN_BARCODE2");
                final int barcodeType = intent.getIntExtra("SCAN_BARCODE_TYPE", -1); // -1:unknown
                final String scanStatus=intent.getStringExtra("SCAN_STATE");
                if("ok".equals(scanStatus)){
                    //成功
                    tv_broadcast_result.setText(scanResult_1);
                }else{
                    //失败如超时等
                }
            }
        };
    }



    private void initView(){


        tv_broadcast_result=(TextView)findViewById(R.id.tv_broadcast_result);
        et_emulator_result=(EditText)findViewById(R.id.et_emulator_result);
        radio_group=(RadioGroup)findViewById(R.id.radio_group);
        btn_reset=(Button)findViewById(R.id.btn_reset);
        btn_reset.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                et_emulator_result.setText("");
                tv_broadcast_result.setText("广播输出的结果显示区");
                tv_broadcast_result.setTextColor(getResources().getColor(android.R.color.black));
            }
        });
        //可选的输出模式设置
        radio_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                Intent intent = new Intent ("ACTION_BAR_SCANCFG");
                switch (checkedId) {

                    //广播方式输出
                    case R.id.radio_broadcast:
                        intent.putExtra("EXTRA_SCAN_MODE", 3);
                        break;

                    //模拟键方式输出
                    case R.id.radio_emulate_key:
                        intent.putExtra("EXTRA_SCAN_MODE", 2);
                        break;

                    //直接填充
                    case R.id.radio_fill:
                        intent.putExtra("EXTRA_SCAN_MODE", 1);
                        break;
                }
                mContext.sendBroadcast(intent);
            }
        });

    }

    private void registerReceiver()
    {
        IntentFilter mFilter= new IntentFilter("nlscan.action.SCANNER_RESULT");
        registerReceiver(mReceiver, mFilter);
    }

    private void unRegisterReceiver()
    {
        try {
            unregisterReceiver(mReceiver);
        } catch (Exception e) {
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        unRegisterReceiver();
    }

    @Override
    protected void onResume() {
        super.onResume();
        registerReceiver();
    }
}
